<?php

/**
 * Subclass for representing a row from the 'user' table.
 *
 * 
 *
 * @package lib.model
 */ 
class User extends BaseUser
{
    /**
     * returns new or existing user
     *
     * @param unknown_type $userId
     * @return unknown
     */
    public function getIsAdmin()
    {
        
        $pattern = '/\b'. MyConstants::ROLE_ADMIN . '\b/';
        
        //debugvar($this->roles);
        if( preg_match($pattern, $this->roles) )
        {
            //debugvar('true');
            return 1;
        }

        return 0;
    }
    
    /**
     * Retrieves user by PK or empty
     *
     * @param integer $userId ID in `user` table
     * @return User
     */
    public static function getOrCreate($userId)
    {
        $user = UserPeer::retrieveByPK($userId);
        if( !$user )
        {
            $user = new User();
        }
        return $user;
    }

    public function getArRoles()
    {
        $ar = array();
        if( $this->getRoles() )
        {
            $arUserRoles = explode(':', $this->getRoles());
            $totalR = count($arUserRoles);
            for($i = 0; $i < $totalR; $i++)
                $ar[$arUserRoles[$i]] = 1;
        }
        return $ar;
    }

    /**
    *   Checks if desired culture is allowed (in app_lng_in_use). Returns default culture in case error
    *   @param string $culture - culture to check (lt, ru, etc)
    *   @return string allowed culture for project
    */
    public static function getValidCultureOrDefault($culture)
    {
        $default = 'lt';
        $arLng = sfConfig::get('app_lng_in_use');

        if( empty( $arLng[$culture] ) )
            $culture = $default;

        return $culture;
    }
    
    /**
     * returns list of active users
     *
     * @return unknown
     */
    public static function getActiveList()
    {
        $c = new Criteria();
        // TODO: add Status
        //$c->add(UserPeer::STATUS_ID, MyConstants::OBJ_STATUS_ACTIVE);
        return UserPeer::doSelect($c);    
    }

    public function __toString()
    {
        return sprintf('%s [%d]', $this->email, $this->id);
    }
    public function getStatus()
    {
        return MyConstants::getStatusName($this->getStatusId());
    }
}
